﻿namespace FormsServer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using WPForms.Common.Model;
    using System.Collections.ObjectModel;

    /// <summary>
    /// Proof of concept wcf service implementation of the <see cref="WPForms.Common.Services.IFormStore"/> interface.
    /// </summary>
    public class FormStore : WPForms.Common.Services.IFormStore
    {
        /// <summary>
        /// Since this is just a POC, we will store all data iin memory.
        /// </summary>
        private static List<FormInstance> forms = new List<FormInstance>();

        /// <summary>
        /// Creates a new form in the store.
        /// </summary>
        /// <param name="instanceDescription">The instance description.</param>
        /// <returns></returns>
        public WPForms.Common.Model.FormInstance CreateForm(FormInstanceDescription instanceDescription)
        {
            // since this is just a POC we don't bother with locking
            // otherwise this would be a serious issue
            int id = 1;
            if (forms.Count > 0)
            {
                id = forms.Max(f => f.Id) + 1;
            }

            var form = new FormInstance(instanceDescription.InstanceName)
            {
                Id = id,
            };

            forms.Add(form);
            return form;
        }

        /// <summary>
        /// Deletes the given form from the store.
        /// </summary>
        /// <param name="form">The form instance to delete.</param>
        public void DeleteForm(FormInstance form)
        {
            var toRemove = forms.SingleOrDefault(f => f.Id == form.Id);
            if (toRemove != null)
            {
                forms.Remove(toRemove);
            }
        }

        /// <summary>
        /// Returns the list of form instances present in the store.
        /// </summary>
        /// <returns></returns>
        public ReadOnlyCollection<FormInstanceDescription> GetForms()
        {
            return forms.Select(
                f => new FormInstanceDescription()
                {
                    Id = f.Id,
                    InstanceName = f.Name,
                })
                .ToList()
                .AsReadOnly();
        }

        /// <summary>
        /// Loads a form identified by its instance description.
        /// </summary>
        /// <param name="description">The description.</param>
        /// <returns></returns>
        public FormInstance LoadForm(FormInstanceDescription description)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Updates the form in the store.
        /// </summary>
        /// <param name="form">The form instance to update.</param>
        public void UpdateForm(FormInstance form)
        {
            var existingForm = forms.SingleOrDefault(f => f.Id == form.Id);
            if (existingForm != null)
            {
                forms.Remove(existingForm);
            }

            forms.Add(form);
        }

        /// <summary>
        /// Loads a form using its id.
        /// </summary>
        /// <param name="formId">The id of the form to load.</param>
        /// <returns></returns>
        public FormInstance LoadForm(int formId)
        {
            return forms.SingleOrDefault(f => f.Id == formId);
        }
    }
}
